define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            $(".btn-edit").data("area", ["50%", "60%"]);

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user.user/index',
                    add_url: 'user.user/add',
                    // edit_url: 'user.user/edit',
                    del_url: 'user.user/del',
                    multi_url: 'user.user/multi',
                    recharge_url: 'user.user/recharge',
                    rechargerecord_url: 'user.user/rechargerecord',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {
                            field: 'avatar',
                            title: __('Avatar'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image,
                            operate: false
                        },
                        // {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        // {field: 'label_ids', title: __('LabelIds'),  searchList: Config.labellist,operate: 'FIND_IN_SET', formatter: Table.api.formatter.label},
                        // {field: 'level', title: __('Level'),  searchList: Config.userlevellabellist,operate: 'FIND_IN_SET', formatter: Table.api.formatter.label},
                        // {
                        //     field: 'viptime',
                        //     title: '会员到期日期',
                        //     formatter: Table.api.formatter.datetime,
                        //     operate: false,
                        //     addclass: 'datetimerange',
                        // },
                        // {field: 'vipday', title: '剩余天数', operate: false},
                        // {field: 'xieyi', title: '剩余协议分数', operate:false},
                        // {field: 'is_auth', title: __('IsAuth'),  formatter: Table.api.formatter.status,searchList: {0: __('Auth 0'), 1: __('Auth 1')}},
                        {
                            field: 'prevtime',
                            title: __('Prevtime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {normal: __('Normal'), hidden: __('Hidden')}
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            // width: '250px',
                            buttons: [
                                // {
                                //     name: 'recharge',
                                //     classname: 'btn btn-xs btn-warning  btn-dialog btn-recharge',
                                //     icon: 'fa fa-pencil',
                                //     url: 'user.user/recharge',
                                //     text: '充/退',
                                //     extend: 'data-toggle="tooltip" data-area=["800px","600px"]',
                                // },
                                // {
                                //     name: 'rechargerecordlist',
                                //     classname: 'btn btn-xs btn-info  btn-dialog btn-rechargerecord',
                                //     icon: 'fa fa-align-justify',
                                //     url: 'user.user/rechargerecordlist',
                                //     text: '充值记录',
                                //     extend: 'data-toggle="tooltip" data-area=["100%","100%"]',
                                // },
                            ]

                        }
                    ]
                ]
            });


            table.on('post-body.bs.table',function(){
                $(".btn-editone").data("area",["600px","400px"]);
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        rechargerecordlist: function () {
            $(".btn-edit").data("area", ["50%", "60%"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'usermoneylog/index',
                    add_url: 'user.user/add',
                    edit_url: 'user.user/edit',
                    del_url: 'user.user/del',
                    multi_url: 'user.user/multi',
                    recharge_url: 'user.user/recharge',
                    rechargerecordlist_url: 'user.user/rechargerecordlist',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url+'?user_id='+table.data('user_id'),
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible:false,
                showRefresh:false,
                showToggle:false,
                showColumns:false,
                showExport:false,
                commonSearch:false,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'userinfo.username', title: '用户名称',operate:"LIKE"},
                        {field: 'userinfo.mobile', title: '手机号',operate:"LIKE"},
                        {field: 'oper_type', title:'操作类型', searchList: {"1":'充值',"2":'退款'}, formatter: Table.api.formatter.normal},
                        {field: 'user_level_type', title:'会员类型',searchList: Config.userlevellabellist,operate: 'FIND_IN_SET', formatter: Table.api.formatter.label},
                        {field: 'level_day', title: '会员时长',operate:false},
                        {field: 'xieyi_num', title:'协议份数',operate:false},
                        {field: 'money', title:'支付/退款金额',operate:false},
                        {field: 'after_level_day', title:'结余会员时长',operate:false},
                        {field: 'after_xieyi_num', title: '结余协议份数',operate:false},
                        {field: 'createtime', title: '创建时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    ]
                ]
            });


            table.on('post-body.bs.table',function(){
                $(".btn-editone").data("area",["600px","400px"]);
            })


            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        recharge: function () {
            Controller.api.bindevent();
        },

        // rechargerecord: function () {
        //     Controller.api.bindevent();
        // },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
